/**      
 * @file		functionsCallingFunctions.cpp
 * @description		This example demonstrates user defined functions
 * 			calling another user defined function. Note 
 *			'getSubsAndtorpedoes()' called from within 
 *			'setDepthCharges()'.
 * @course		CSCI 123 Section 00000
 * @assignment		Sample 6
 * @date		mm/dd/yyyy
 * @author		Brad Rippe (00000000) brippe@fullcoll.edu
 * @version		1.0
 */
#include <iostream>
#include <cstdlib>
using namespace std;

/**
 * Expects the user to input integer values for submarines and 
 * torpedoes. This function doesn't prompt the user for input.
 * 
 * @param	aSubmarines the variable for submarines
 * @param	aTorpedoes the variable for torpedoes
 * @pre		submarines and torpedoes have been declared
 * @post	submarines and torpedoes have been set by the user
 */
void getSubsAndTorpedoes(int& aSubmarines, int& aTorpedoes);

/**
 * This function randomly attempts to hit submarines with depth charges 
 * @param	aSubmarines the number of submarines to hit
 * @param	aTorpedoes the number of torpedoes each submarine has
 * @param	aDepthCharges the number of depth charges dropped on the subs
 * @pre		the arguments have been initialized
 * @post	the submarines have been fired upon with depth charges, reducing
 *		the amount of submarines by 1 for each depth charge that hit a 
 *		sub
 */
void setDepthCharges(int& aSubmarines, int& aTorpedoes, int aDepthCharges);

/**
 * @return zero if the application executes successfully
 */
int main() {
	int submarines = 0;
	int torpedoes = 0;
	int depthCharges = 10;

	cout << "Type the number of submarines and torpedoes\n";
	setDepthCharges(submarines, torpedoes, depthCharges);
	cout << "There are only " << submarines << " left!\n";
	return 0;
}

void getSubsAndTorpedoes(int& aSubmarines, int& aTorpedoes) {
	cin >> aSubmarines;
	cin >> aTorpedoes;
	cout << "You have typed ";
	cout << aSubmarines << " submarines and ";
	cout << aTorpedoes << " torpedoes\n";
}

void setDepthCharges(int& aSubmarines, int& aTorpedoes, int aDepthCharges) {

	// calling a function inside a function
	getSubsAndTorpedoes(aSubmarines, aTorpedoes); 

	for(int i = 0; i < aDepthCharges && 0 < aSubmarines; i++) {
		// to produce integers in the range of
		// 0 - 9, we mod by 10
		// multiplying the submarines by 2 adds some
		// variability that the depth charge may miss
		// the submarine
		int hitSub = 1 + rand() % (aSubmarines * 2);
		if(hitSub <= aSubmarines) {
			cout << "Submarine " << hitSub << " has been hit by ";
			cout << "a depth charge\n";
			aSubmarines--;
		} else {
			cout << "Depth charge " << (i+1) << " missed.\n";
		}
	}
}
